<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    
    <title>Compile Kernel | jouyouyun&#39;s blog</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1">
    <meta name="description" content="最近由于要调试 usbhid 模块，所以需要编译 kernel ，在此整理下方法，以免每次都查找。
Dependencies


deepin
sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev


archlinux
sudo pacman -S gcc ncurses bison flex openssl libelf elfutils


若编译时有报错，请按照错误提示按照缺失的依赖。">
    <meta name="generator" content="Hugo 0.105.0">
    
    
    
    
      <meta name="robots" content="noindex, nofollow">
    

    
<link rel="stylesheet" href="/ananke/css/main.min.css" >



    
    
    
      

    

    
    
    <meta property="og:title" content="Compile Kernel" />
<meta property="og:description" content="最近由于要调试 usbhid 模块，所以需要编译 kernel ，在此整理下方法，以免每次都查找。
Dependencies


deepin
sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev


archlinux
sudo pacman -S gcc ncurses bison flex openssl libelf elfutils


若编译时有报错，请按照错误提示按照缺失的依赖。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://jouyouyun.github.io/post/linux-kernel-compile/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2019-08-06T21:38:21+08:00" />
<meta property="article:modified_time" content="2019-11-27T20:05:41+08:00" />

<meta itemprop="name" content="Compile Kernel">
<meta itemprop="description" content="最近由于要调试 usbhid 模块，所以需要编译 kernel ，在此整理下方法，以免每次都查找。
Dependencies


deepin
sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev


archlinux
sudo pacman -S gcc ncurses bison flex openssl libelf elfutils


若编译时有报错，请按照错误提示按照缺失的依赖。"><meta itemprop="datePublished" content="2019-08-06T21:38:21+08:00" />
<meta itemprop="dateModified" content="2019-11-27T20:05:41+08:00" />
<meta itemprop="wordCount" content="478">
<meta itemprop="keywords" content="kernel,compile,linux," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Compile Kernel"/>
<meta name="twitter:description" content="最近由于要调试 usbhid 模块，所以需要编译 kernel ，在此整理下方法，以免每次都查找。
Dependencies


deepin
sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev


archlinux
sudo pacman -S gcc ncurses bison flex openssl libelf elfutils


若编译时有报错，请按照错误提示按照缺失的依赖。"/>

	
  </head>

  <body class="ma0 avenir bg-near-white">

    
   
  

  <header>
    <div class="bg-black">
      <nav class="pv3 ph3 ph4-ns" role="navigation">
  <div class="flex-l justify-between items-center center">
    <a href="/" class="f3 fw2 hover-white no-underline white-90 dib">
      
        jouyouyun&#39;s blog
      
    </a>
    <div class="flex-l items-center">
      

      
        <ul class="pl0 mr3">
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/post/" title="Archives 页">
              Archives
            </a>
          </li>
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/tags/" title="Tags 页">
              Tags
            </a>
          </li>
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/categories/" title="Categories 页">
              Categories
            </a>
          </li>
          
        </ul>
      
      
<div class="ananke-socials">
  
    <a href="https://github.com/jouyouyun" target="_blank" class="github ananke-social-link link-transition stackoverflow link dib z-999 pt3 pt0-l mr1" title="GitHub link" rel="noopener" aria-label="follow on GitHub——Opens in a new window">
      
        <span class="icon"><svg style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <path d="M256,32C132.3,32,32,134.8,32,261.7c0,101.5,64.2,187.5,153.2,217.9c11.2,2.1,15.3-5,15.3-11.1   c0-5.5-0.2-19.9-0.3-39.1c-62.3,13.9-75.5-30.8-75.5-30.8c-10.2-26.5-24.9-33.6-24.9-33.6c-20.3-14.3,1.5-14,1.5-14   c22.5,1.6,34.3,23.7,34.3,23.7c20,35.1,52.4,25,65.2,19.1c2-14.8,7.8-25,14.2-30.7c-49.7-5.8-102-25.5-102-113.5   c0-25.1,8.7-45.6,23-61.6c-2.3-5.8-10-29.2,2.2-60.8c0,0,18.8-6.2,61.6,23.5c17.9-5.1,37-7.6,56.1-7.7c19,0.1,38.2,2.6,56.1,7.7   c42.8-29.7,61.5-23.5,61.5-23.5c12.2,31.6,4.5,55,2.2,60.8c14.3,16.1,23,36.6,23,61.6c0,88.2-52.4,107.6-102.3,113.3   c8,7.1,15.2,21.1,15.2,42.5c0,30.7-0.3,55.5-0.3,63c0,6.1,4,13.3,15.4,11C415.9,449.1,480,363.1,480,261.7   C480,134.8,379.7,32,256,32z"/>
</svg>
</span>
      
<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;"/>
</svg>
</span></a>
  
</div>

    </div>
  </div>
</nav>

    </div>
  </header>



    <main class="pb7" role="main">
      
  
  <article class="flex-l flex-wrap justify-between mw8 center ph3">
    <header class="mt4 w-100">
      <aside class="instapaper_ignoref b helvetica tracked">
          
        POSTS
      </aside>
      










  <div id="sharing" class="mt3 ananke-socials">
    
  </div>


      <h1 class="f1 athelas mt3 mb1">Compile Kernel</h1>
      
      <p class="tracked">
         <strong>jouyouyun</strong>
      </p>
      
      
      
      <time class="f6 mv4 dib tracked" datetime="2019-08-06T21:38:21+08:00">八月 6, 2019</time>
      

      
      
    </header>
    <div class="nested-copy-line-height lh-copy serif f4 nested-links mid-gray pr4-l w-two-thirds-l"><p>最近由于要调试 <code>usbhid</code> 模块，所以需要编译 <code>kernel</code> ，在此整理下方法，以免每次都查找。</p>
<h2 id="dependencies">Dependencies</h2>
<ul>
<li>
<p><code>deepin</code></p>
<p><code>sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev</code></p>
</li>
<li>
<p><code>archlinux</code></p>
<p><code>sudo pacman -S gcc ncurses bison flex openssl libelf elfutils</code></p>
</li>
</ul>
<p>若编译时有报错，请按照错误提示按照缺失的依赖。</p>
<hr>
<h2 id="configuration">Configuration</h2>
<p>默认情况下使用当前内核的配置，操作如下：</p>
<ul>
<li>
<p><code>deepin</code></p>
<p><code>cp /boot/config-`uname -r` .config</code></p>
</li>
<li>
<p><code>archlinux</code></p>
<p><code>zcat /proc/config.gz &gt; .config</code></p>
</li>
</ul>
<p>如需自定义，执行 <code>make nconfig</code> 修改。</p>
<hr>
<h3 id="version">Version</h3>
<p>编译时需要指定版本，不然版本不同模块会加载失败，版本指定的方法如下：</p>
<h4 id="vermagic">vermagic</h4>
<p>内核编译时会设置 <code>vermagic</code> ，如果模块的 <code>vermagic</code> 不一致则会加载失败，使用 <code>modinfo &lt;module&gt;</code> 可查看当前值。</p>
<p>对于 <code>vermagic</code> 的设置，目前只找到了通过变量设置的方法，如下：</p>
<p>有 <code>vermagic</code> 为 <code>4.19.0-5-amd64</code> ，可通过变量 <code>VERSION, PATCHLEVEL, SUBLEVEL and EXTRAVERSION</code> 来指定它，详细如下：</p>
<ul>
<li>VERSION : 对应的是 <code>4</code></li>
<li>PATCHLEVEL : 对应的是 <code>19</code></li>
<li>SUBLEVEL : 对应的是 <code>0</code></li>
<li>EXTRAVERSION : 对应的是 <code>-5-amd64</code></li>
</ul>
<p>其中 <code>EXTRAVERSION</code> 还可通过 <code>.config</code> 中 <code>CONFIG_LOCALVERSION</code> 指定(需要确保 <code>CONFIG_LOCALVERSION_AUTO</code> 不被设置)</p>
<p>或者这样指定：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>make KERNELRELEASE<span style="color:#f92672">=</span>4.19.34-2deepin-generic ARCH<span style="color:#f92672">=</span>arm64   KBUILD_BUILD_VERSION<span style="color:#f92672">=</span><span style="color:#ae81ff">20</span> KBUILD_SRC<span style="color:#f92672">=</span>
</span></span></code></pre></div><h4 id="avoid-add-plus-after-kernel-version">Avoid add &lsquo;+&rsquo; after kernel version</h4>
<blockquote>
<p>To prevent the script scripts/setlocalversion to append the + to the end of the kernel local version, create an empty .scmversion file in the root of the kernel sources.</p>
<p>$ touch .scmversion</p>
<p>this way, you&rsquo;ll be able to leave LOCALVERSION as is in the kernel configuration file, in case you want to append a local signature to the kernel name.</p>
</blockquote>
<hr>
<h2 id="build">Build</h2>
<p>The kernel version: <strong>4.19.37</strong> , vermagic: <strong>4.19.0-5-amd64</strong></p>
<h3 id="prepare">Prepare</h3>
<p><code>make VERSION=4 PATCHLEVEL=19 SUBLEVEL=0 EXTRAVERSION='-5-amd64' scripts prepare modules_prepare -j 24</code></p>
<h3 id="make">Make</h3>
<p><code>make VERSION=4 PATCHLEVEL=19 SUBLEVEL=0 EXTRAVERSION='-5-amd64' -j 24</code></p>
<h3 id="build-special-module">Build Special Module</h3>
<h4 id="prepare">Prepare</h4>
<p><code>make VERSION=4 PATCHLEVEL=19 SUBLEVEL=0 EXTRAVERSION='-5-amd64' scripts modules_prepare -j 24</code></p>
<h4 id="make">Make</h4>
<p><code>make VERSION=4 PATCHLEVEL=19 SUBLEVEL=0 EXTRAVERSION='-5-amd64' -C . M=&lt;module path&gt;</code></p>
<p>例如编译 <code>usbhid.ko</code> 则是： <code>make VERSION=4 PATCHLEVEL=19 SUBLEVEL=0 EXTRAVERSION='-5-amd64' -C . M=drivers/hid/usbhid</code></p>
<hr>
<h2 id="installation">Installation</h2>
<h3 id="modules">Modules</h3>
<p><code>sudo make modules_install</code></p>
<p>可通过 <code>INSTALL_MOD_PATH</code> 指定 <code>modules</code> 安装目录</p>
<p>然后执行以下操作：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>sudo depmod <span style="color:#75715e"># 更新 modules 映射</span>
</span></span><span style="display:flex;"><span>sudo update-initramfs  -u <span style="color:#75715e"># deepin 下更新当前内核，使用 &#39;-k&#39; 指定内核</span>
</span></span><span style="display:flex;"><span>sudo mkinitcpio -p &lt;preset&gt; <span style="color:#75715e"># archlinux 下更新指定内核， &#39;preset&#39; 在 &#39;/etc/mkinitcpio.d&#39; 中查看</span>
</span></span></code></pre></div><hr>
<h3 id="image">Image</h3>
<p><code>sudo make install</code></p>
<p>目的是生成下面的文件：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>initramfs-4.19.0-5-amd64.img
</span></span><span style="display:flex;"><span>System.map-4.19.0-5-amd64
</span></span><span style="display:flex;"><span>vmlinuz-4.19.0-5-amd64
</span></span></code></pre></div><p>如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>$ sudo make install -j <span style="color:#ae81ff">4</span>
</span></span><span style="display:flex;"><span>sh ./arch/x86/boot/install.sh 4.19.0-5-amd64 arch/x86/boot/bzImage <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>        System.map <span style="color:#e6db74">&#34;/boot&#34;</span>
</span></span><span style="display:flex;"><span>run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 4.19.0-5-amd64 /boot/vmlinuz-4.19.0-5-amd64
</span></span><span style="display:flex;"><span>run-parts: executing /etc/kernel/postinst.d/dkms 4.19.0-5-amd64 /boot/vmlinuz-4.19.0-5-amd64
</span></span><span style="display:flex;"><span>run-parts: executing /etc/kernel/postinst.d/initramfs-tools 4.19.0-5-amd64 /boot/vmlinuz-4.19.0-5-amd64
</span></span><span style="display:flex;"><span>update-initramfs: Generating /boot/initrd.img-4.19.0-5-amd64
</span></span><span style="display:flex;"><span>cryptsetup: WARNING: The initramfs image may not contain cryptsetup binaries
</span></span><span style="display:flex;"><span>    nor crypto modules. If that<span style="color:#e6db74">&#39;s on purpose, you may want to uninstall the
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">    &#39;</span>cryptsetup-initramfs<span style="color:#e6db74">&#39; package in order to disable the cryptsetup initramfs
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">    integration and avoid this warning.
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">setupcon is missing. Please install the &#39;</span>console-setup<span style="color:#960050;background-color:#1e0010">&#39;</span> package.
</span></span><span style="display:flex;"><span>W: plymouth: The plugin label.so is missing, the selected theme might not work as expected.
</span></span><span style="display:flex;"><span>W: plymouth: You might want to install the plymouth-themes package to fix this.
</span></span><span style="display:flex;"><span>I: The initramfs will attempt to resume from /dev/sda1
</span></span><span style="display:flex;"><span>I: <span style="color:#f92672">(</span>UUID<span style="color:#f92672">=</span>0d78d207-61eb-4e99-b7d8-a2b2cb2515d6<span style="color:#f92672">)</span>
</span></span><span style="display:flex;"><span>I: Set the RESUME variable to override this.
</span></span><span style="display:flex;"><span>run-parts: executing /etc/kernel/postinst.d/reconfigure-dde-daemon 4.19.0-5-amd64 /boot/vmlinuz-4.19.0-5-amd64
</span></span><span style="display:flex;"><span>fix systemd/logind config
</span></span><span style="display:flex;"><span>run-parts: executing /etc/kernel/postinst.d/vboxadd 4.19.0-5-amd64 /boot/vmlinuz-4.19.0-5-amd64
</span></span><span style="display:flex;"><span>run-parts: executing /etc/kernel/postinst.d/zz-update-grub 4.19.0-5-amd64 /boot/vmlinuz-4.19.0-5-amd64
</span></span><span style="display:flex;"><span>Generating grub configuration file ...
</span></span><span style="display:flex;"><span>Found theme: /boot/grub/themes/deepin-fallback/theme.txt
</span></span><span style="display:flex;"><span>Found linux image: /boot/vmlinuz-4.19.0-5-amd64
</span></span><span style="display:flex;"><span>Found initrd image: /boot/initrd.img-4.19.0-5-amd64
</span></span><span style="display:flex;"><span>Found linux image: /boot/vmlinuz-4.19.0-5-amd64.old
</span></span><span style="display:flex;"><span>Found initrd image: /boot/initrd.img-4.19.0-5-amd64
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">done</span>
</span></span></code></pre></div><hr>
<h2 id="参考文档">参考文档</h2>
<ul>
<li><a href="https://wiki.archlinux.org/index.php/Kernel/Traditional%5Fcompilation">Traditional_compilation</a></li>
<li><a href="https://wiki.archlinux.org/index.php/Compile%5Fkernel%5Fmodule">Compile kernel module</a></li>
<li><a href="https://www.cyberciti.biz/tips/compiling-linux-kernel-26.html">How to compile and install Linux Kernel 5.1.14 from source code</a></li>
<li><a href="https://yoursunny.com/t/2018/one-kernel-module/">How to Compile Just One Kernel Module</a></li>
<li><a href="http://www.linuxfromscratch.org/lfs/view/development/chapter06/linux-headers.html">Installation of Linux API Headers</a></li>
<li><a href="https://linux.die.net/lkmpg/x380.html">Building modules for a precompiled kernel</a></li>
<li><a href="https://www.acmesystems.it/compile%5Flinux%5Fkernel">Compile the Linux Kernel from sources</a></li>
</ul><ul class="pa0">
  
   <li class="list di">
     <a href="/tags/kernel" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">kernel</a>
   </li>
  
   <li class="list di">
     <a href="/tags/compile" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">compile</a>
   </li>
  
   <li class="list di">
     <a href="/tags/linux" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">linux</a>
   </li>
  
</ul>
<div class="mt6 instapaper_ignoref">
      
      
      </div>
    </div>

    <aside class="w-30-l mt6-l">




</aside>

  </article>

    </main>
    <footer class="bg-black bottom-0 w-100 pa3" role="contentinfo">
  <div class="flex justify-between">
  <a class="f4 fw4 hover-white no-underline white-70 dn dib-ns pv2 ph3" href="https://jouyouyun.github.io" >
    &copy;  jouyouyun 2023 
  </a>
    <div>
<div class="ananke-socials">
  
    <a href="https://github.com/jouyouyun" target="_blank" class="github ananke-social-link link-transition stackoverflow link dib z-999 pt3 pt0-l mr1" title="GitHub link" rel="noopener" aria-label="follow on GitHub——Opens in a new window">
      
        <span class="icon"><svg style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <path d="M256,32C132.3,32,32,134.8,32,261.7c0,101.5,64.2,187.5,153.2,217.9c11.2,2.1,15.3-5,15.3-11.1   c0-5.5-0.2-19.9-0.3-39.1c-62.3,13.9-75.5-30.8-75.5-30.8c-10.2-26.5-24.9-33.6-24.9-33.6c-20.3-14.3,1.5-14,1.5-14   c22.5,1.6,34.3,23.7,34.3,23.7c20,35.1,52.4,25,65.2,19.1c2-14.8,7.8-25,14.2-30.7c-49.7-5.8-102-25.5-102-113.5   c0-25.1,8.7-45.6,23-61.6c-2.3-5.8-10-29.2,2.2-60.8c0,0,18.8-6.2,61.6,23.5c17.9-5.1,37-7.6,56.1-7.7c19,0.1,38.2,2.6,56.1,7.7   c42.8-29.7,61.5-23.5,61.5-23.5c12.2,31.6,4.5,55,2.2,60.8c14.3,16.1,23,36.6,23,61.6c0,88.2-52.4,107.6-102.3,113.3   c8,7.1,15.2,21.1,15.2,42.5c0,30.7-0.3,55.5-0.3,63c0,6.1,4,13.3,15.4,11C415.9,449.1,480,363.1,480,261.7   C480,134.8,379.7,32,256,32z"/>
</svg>
</span>
      
<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;"/>
</svg>
</span></a>
  
</div>
</div>
  </div>
</footer>

  </body>
</html>
